package com.veltri.JDroid;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;

//class creates three tabs where an activity running in each tab
public class MainActivity extends TabActivity {
	// declare object of MainActivity
	MainActivity self;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		// set view to homescreen (tabs screen)
		setContentView(R.layout.homescreen);

		// initialize self to this MainActivity for later reference
		self = this;

		// get resources to create the TabHost
		Resources res = getResources();
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;

		// Reusable Intent for each tab
		Intent intent;
		
		((MyApplication) self.getApplication()).setCategoryNames();

		// initialize intent for first tab and set the tab attributes
		intent = new Intent().setClass(this, TabOneActivity.class);
		spec = tabHost
				.newTabSpec("CategoryOne")
				.setIndicator(((MyApplication) self.getApplication()).getCategoryName(0),
						res.getDrawable(R.drawable.ic_tab_songs))
				.setContent(intent);
		tabHost.addTab(spec);

		// initialize intent for second tab and set the tab attributes
		intent = new Intent().setClass(this, TabTwoActivity.class);
		spec = tabHost
				.newTabSpec("categoryTwo")
				.setIndicator(((MyApplication) self.getApplication()).getCategoryName(1),
						res.getDrawable(R.drawable.ic_tab_movies))
				.setContent(intent);
		tabHost.addTab(spec);

		// initialize intent for third tab and set the tab attributes
		intent = new Intent().setClass(this, TabThreeActivity.class);
		spec = tabHost
				.newTabSpec("categoryThree")
				.setIndicator(((MyApplication) self.getApplication()).getCategoryName(2),
						res.getDrawable(R.drawable.ic_tab_images))
				.setContent(intent);
		tabHost.addTab(spec);

		// set the current tab the last (third) one
		int tab = getIntent().getIntExtra("TAB_INDEX", 2);
		tabHost.setCurrentTab( tab);
	}

	// shows help dialog when button is clicked under options menu
	public void showHelpDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(
				"This is a Joomla Extension, designed to make "
						+ "Joomla powered websites more accessible on mobile"
						+ "devices. Created by Nick Bruno and Vincent Veltri, and managed by"
						+ " Timothy Joyce").setCancelable(false)
				.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}

	// used to inflate options menu (bring up) when clicked
	// menu can be pressed again to deflate menu (handled by android)
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();

		inflater.inflate(R.menu.menu, menu);

		return true;

	}

	// create options menu
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.about:
			showHelpDialog();
			return true;
		case R.id.logout:
			// Clear session information
			((MyApplication) self.getApplication()).logout();
			// runs LoginActivity again
			// credentials will be removed from username/password textfields
			Intent myIntent = new Intent(this, LoginActivity.class);
			startActivityForResult(myIntent, 0);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/*@Override
	public void onPause() {
		super.onPause();
		finish();
	}*/
}